package util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "Filter 0",urlPatterns = {"/*"})
public class Filter00_Encoding implements Filter {
    public void init(FilterConfig config) throws ServletException{

    }
    public void doFilter(ServletRequest req, ServletResponse resp,
                         FilterChain chain) throws ServletException, IOException{
        System.out.println("Filter 0 - encoding begins");
        //HttpServletRequest是ServletRequest的子接口
        HttpServletRequest request =(HttpServletRequest) req;
        HttpServletResponse response =(HttpServletResponse) resp;

        String path = request.getRequestURI();
            response.setContentType("application/json;charset=UTF-8");
            String method =request.getMethod();
            //contains区分大小写
            if(!"DELETE".contains(method)){
                request.setCharacterEncoding("UTF-8");
            }
        chain.doFilter(req,resp);
        System.out.println("Filter 0 - encoding ends");
    }
    public void destroy() {
    }
}
